package com.hnhs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hnhs.info.PageInfo;
import com.hnhs.mapper.ProductMapper;
import com.hnhs.model.Product;
import com.hnhs.model.ProductQuery;
import com.hnhs.model.ProductStatusUpdate;
import com.hnhs.threadlocalutil.LocalPageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Autowired
    private ProductMapper mapper;

    public List<Product> findAll(){
        return mapper.findAll();
    }
    public List<Product> select(ProductQuery query){
        if(query.getPageNum()!= null && query.getPageSize() != null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }
        List<Product> selectList = mapper.select(query);
        if(selectList instanceof Page){
            Page page=(Page) selectList;
            Long total= page.getTotal();
            System.out.println(total);
            LocalPageInfoUtil.set(PageInfo.builder().total(total).build());
        }
        return selectList;
    }

    public int insert(Product product){
        return mapper.insert(product);
    }
    public int update(Product product){
        return mapper.update(product);
    }
    public int delete(Integer id){
        return mapper.updateStatus(id,0);
    }

    public Integer updateStatus(ProductStatusUpdate update){
        return mapper.updateStatus(update.getId(),update.getStatus());
    }

}
